package com.steinwurf.hdcjoinwifi;

import com.steinwurf.hdcjoinwifi.CheckSSIDBroadcastReceiver.SSIDFoundListener;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.MatchingSkills;
import ohos.wifi.WifiDevice;
import ohos.wifi.WifiEvents;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Field;

import static org.junit.Assert.assertEquals;

@RunWith(JUnitParamsRunner.class)
public class CheckSSIDBroadcastReceiverTest {

    private static final String TAG = "CheckSSIDBroadcastReceiverTest";
    private CheckSSIDBroadcastReceiver mCheckSSIDBroadcastReceiver;
    private Context mContext;
    private CommonEventSubscribeInfo mCommonEventSubscribeInfo;
    private WifiDevice mWifiDevice;
    private static InterfaceExample listener;


    private static final Object[] getDatas() {
        return new Object[]{
                new Object[]{WifiEvents.EVENT_ACTIVE_STATE, WifiEvents.STATE_ACTIVE, WifiEvents.STATE_CONNECTED},
                new Object[]{WifiEvents.EVENT_ACTIVE_STATE, WifiEvents.STATE_INACTIVE, WifiEvents.STATE_CONNECTED},
                new Object[]{WifiEvents.EVENT_ACTIVE_STATE, WifiEvents.STATE_INACTIVE, WifiEvents.STATE_DISCONNECTED},
                new Object[]{WifiEvents.EVENT_CONN_STATE, WifiEvents.STATE_ACTIVE, WifiEvents.STATE_CONNECTED},
                new Object[]{WifiEvents.EVENT_CONN_STATE, WifiEvents.STATE_INACTIVE, WifiEvents.STATE_DISCONNECTED},
                new Object[]{WifiEvents.EVENT_CONN_STATE, WifiEvents.STATE_ACTIVE, WifiEvents.STATE_DISCONNECTED}
        };
    }

    public class InterfaceExample implements SSIDFoundListener{

        @Override
        public void SSIDFound() {

        }

        @Override
        public void wifiEnabled() {

        }
    }


    @Before
    public void setUp() throws Exception {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        MatchingSkills match = new MatchingSkills();
        match.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
        match.addEvent(WifiEvents.EVENT_CONN_STATE);
        mCommonEventSubscribeInfo = new CommonEventSubscribeInfo(match);
        mWifiDevice = WifiDevice.getInstance(mContext);
        listener = new InterfaceExample();
        mCheckSSIDBroadcastReceiver = new CheckSSIDBroadcastReceiver(mCommonEventSubscribeInfo, mWifiDevice, "test");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void setSSIDFoundListener() {
        mCheckSSIDBroadcastReceiver.setSSIDFoundListener(listener);
        SSIDFoundListener foundListener = null;
        try {
            Field field = mCheckSSIDBroadcastReceiver.getClass().getDeclaredField("mSSIDFoundListener");
            field.setAccessible(true);
            foundListener = (SSIDFoundListener) field.get(mCheckSSIDBroadcastReceiver);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (foundListener != null)
        assertEquals(listener, foundListener);
    }

    @Test
    @Parameters(method = "getDatas")
    public void onReceiveEvent(String str, int wifiState, int connState) {
        CommonEventData commonEventData = new CommonEventData();
//        commonEventData.getIntent().setAction(str);
//        commonEventData.getIntent().getParams().setParam(WifiEvents.PARAM_ACTIVE_STATE,wifiState);
//        commonEventData.getIntent().getParams().setParam(WifiEvents.PARAM_CONN_STATE,connState);
    }
}